package cn.rkylin.oms.common.event.message;

import com.google.common.collect.Maps;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;

import static cn.rkylin.core.utils.VariableExpressionReplacer.variables;

public class MessageDefinition {

    /**
     * id
     */
    private String id;

    /**
     * 消息编号
     */
    private String code;

    /**
     * 消息名称：设置商品分单店铺、自动任务异常、催付通知、发货通知、补发通知
     */
    private String name;

    /**
     * 发送方式
     */
    private SendWay sendWay;

    /**
     * 标题（仅邮件发送方式可用）
     */
    private String title;

    /**
     * 失败可以重试几次
     */
    private Integer retryCount;

    /**
     * 失败重试间隔
     */
    private Integer retryInterval;

    /**
     * 消息模板，例如：{OPERATOR}您好，项目{PROJECT}，店铺{SHOP}，没有设置分单规则，请关注。
     */
    private String template;

    /**
     * 是否启用
     */
    private boolean enabled;

    /**
     * 是否已经删除
     */
    private boolean deleted;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SendWay getSendWay() {
        return sendWay;
    }

    public void setSendWay(SendWay sendWay) {
        this.sendWay = sendWay;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(Integer retryCount) {
        this.retryCount = retryCount;
    }

    public Integer getRetryInterval() {
        return retryInterval;
    }

    public void setRetryInterval(Integer retryInterval) {
        this.retryInterval = retryInterval;
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public String fillVariables(EnumMap<MessageTemplateVariable, Object> variables) {
        Map<String, Object> readableVariables = Maps.newHashMap();
        Set<MessageTemplateVariable> keys = variables.keySet();
        for (MessageTemplateVariable key : keys) {
            readableVariables.put(key.name(), variables.get(key));
        }
        return variables(readableVariables).replace(template);
    }
}
